package com.czxy.service;

import com.czxy.mapper.ImpressionMapper;
import com.czxy.mapper.SkuCommentMapper;
import com.czxy.pojo.Impression;
import com.czxy.pojo.SkuComment;
import com.czxy.vo.CommentResult;
import com.czxy.vo.PageRequest;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;

/**
 * @author 张青卯
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommentService {

    private final SkuCommentMapper skuCommentMapper;

    private final ImpressionMapper impressionMapper;

    @Autowired
    public CommentService(ImpressionMapper impressionMapper, SkuCommentMapper skuCommentMapper) {
        this.impressionMapper = impressionMapper;
        this.skuCommentMapper = skuCommentMapper;
    }

    public CommentResult findPageById(Integer spuid, PageRequest pageRequest) {
        CommentResult result = new CommentResult();
        List<Impression> impressionList = impressionMapper.findById(spuid);
        result.setImpressions(impressionList);
        Integer count = skuCommentMapper.findCountBySpuId(spuid);
        result.setComment_count(count);
        Integer pageNum = pageRequest.getPage();
        Integer pageSize = pageRequest.getLimit();
        PageHelper.startPage(pageNum,pageSize);
        List<SkuComment> commentList = skuCommentMapper.findSelectAll(spuid);
        result.setComments(commentList);
        List<SkuComment> list = skuCommentMapper.findSelectAll(spuid);
        int common = 0;
        int bad = 0;
        for (SkuComment s : list) {
            if (s.getRatio()==1){
                common++;
            }else if (s.getRatio()==2){
                bad++;
            }
        }
        HashMap<String, Integer> map = new HashMap<>(3);
        map.put("common",(common*100)/count);
        map.put("bad",(bad*100)/count);
        map.put("goods",100-map.get("common")-map.get("bad"));
        result.setRatio(map);
        return result;
    }
}